package cn.zjutleo.village_server.controller;

import cn.zjutleo.village_server.annotation.OperationLog;
import cn.zjutleo.village_server.annotation.RequiresLogin;
import cn.zjutleo.village_server.dto.MessageDto;
import cn.zjutleo.village_server.exception.apiException.daoException.SelectException;
import cn.zjutleo.village_server.service.MessageService;
import cn.zjutleo.village_server.util.ThreadLocalUtil;
import cn.zjutleo.village_server.vo.MessageVo;
import cn.zjutleo.village_server.vo.ResponseBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author : Fu Lantian
 * @date : Created in 2021/11/17
 * @description: 站内消息相关控制类
 */
@Api(tags = "站内信息控制类")
@RestController
@RequestMapping("/message")
public class MessageController {

    private static final String MODULE_NAME = "站内消息模块";

    @Resource
    private MessageService messageService;

    @ApiOperation("根据用户id获取用户所有消息")
    @OperationLog(module = MODULE_NAME, method = "根据用户id获取用户所有消息")
    @RequiresLogin
    @GetMapping("/all")
    public ResponseBean<List<MessageVo>> getAllMessageByUserId() {
        Integer userId = ThreadLocalUtil.getCurrentUser();
        List<MessageVo> messageVos = messageService.getAllMessageByUserId(userId);
        return new ResponseBean<>(messageVos);
    }

    @ApiOperation("发送系统消息")
    @OperationLog(module = MODULE_NAME, method = "发送系统消息")
    @RequiresLogin
    @PostMapping("/send/system")
    public ResponseBean<Object> sendSystemMessage(@RequestBody @Validated MessageDto messageDto) {
        messageService.sendSystemMessage(messageDto);
        return new ResponseBean<>(null);
    }

    @ApiOperation("发送私信")
    @OperationLog(module = MODULE_NAME, method = "发送私信")
    @RequiresLogin
    @PostMapping("/send/private")
    public ResponseBean<Object> sendPrivateMessage(@RequestBody @Validated MessageDto messageDto) {
        messageService.sendPrivateMessage(messageDto);
        return new ResponseBean<>(null);
    }

    @ApiOperation("修改消息状态")
    @OperationLog(module = MODULE_NAME, method = "修改消息状态")
    @RequiresLogin
    @GetMapping("/status/{messageId}")
    public ResponseBean<Object> changeMessageStatus(@PathVariable Integer messageId) {
        messageService.changeMessageStatusById(messageId);
        return new ResponseBean<>(null);
    }

    @ApiOperation("获取用户所有消息条数")
    @OperationLog(module = MODULE_NAME, method = "获取用户所有消息条数")
    @RequiresLogin
    @GetMapping("/account/all")
    public ResponseBean<Integer> getAllMessageAccount() {
        Integer userId = ThreadLocalUtil.getCurrentUser();
        return new ResponseBean<>(messageService.getAllMessageAccountByUserId(userId));
    }

    @ApiOperation("根据消息类型获取用户该类型未读消息条数")
    @OperationLog(module = MODULE_NAME, method = "根据消息类型获取用户该类型未读消息条数")
    @RequiresLogin
    @GetMapping("/account/notRead/{type}")
    public ResponseBean<Integer> getNotReadMessageAccountByType(@PathVariable String type) throws SelectException {
        Integer userId = ThreadLocalUtil.getCurrentUser();
        return new ResponseBean<>(messageService.getNotReadMessageAccountByUserIdAndType(userId, type));
    }
}
